﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pharmacy.Data;
using Pharmacy.Data.Entity;
using System.Transactions;
using Pharmacy.Data.Repositories;
using Pharmacy.Data.Core;
using Pharmacy.Ultilities.Resources;
using Pharmacy.Ultilities;


namespace Pharmacy.Business
{
    public partial class UnitOfMeasurGroupBO : BaseBusiness
    {
        #region Constructors
        public UnitOfMeasurGroupBO(IObjectContext objectContext) : base(objectContext) { }
        public UnitOfMeasurGroupBO() : base() { }
        #endregion

        #region Private Methods


        #endregion

        #region Public Methods
        public UnitOfMeasureGroupRepository ListUnitOfMeasureGroup()
        {
            UnitOfMeasureGroupRepository lstUnitOfMeasureGroup = new UnitOfMeasureGroupRepository(PharmacyContext, MergeOption.Overrive);
            return lstUnitOfMeasureGroup;

        }

        public void CreateNewUnitOfMeasureGroup(UnitOfMeasureGroup addedObject)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    UnitOfMeasureGroupRepository lst = new UnitOfMeasureGroupRepository(PharmacyContext);
                    if (addedObject != null)
                    {
                        lst.Add(addedObject);
                        PharmacyContext.SaveChanges();
                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public void SaveUnitOfMeasureGroup(UnitOfMeasureGroup editObject)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                        PharmacyContext.SaveChanges();
                        scope.Complete();                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public void DeleteUnitOfMeasureGroup(UnitOfMeasureGroup deleteObject)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    UnitOfMeasureGroupRepository lst = new UnitOfMeasureGroupRepository(PharmacyContext);
                    UnitOfMeasureGroupDetailRepository lstDetail = new UnitOfMeasureGroupDetailRepository(PharmacyContext);
                    if (deleteObject != null)
                    {
                        lstDetail.Delete(deleteObject.UnitOfMeasureGroupDetails.ToList());
                        lst.Delete(deleteObject);
                        PharmacyContext.SaveChanges();
                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        
        #endregion
    }
}
